﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace deWSP.SharePointRoot.Template.CONTROLTEMPLATES.CMAP
{
    /// <summary>
    /// This User Control is just a sample to demonstrate how to use the de Control Container Web Part. 
    /// </summary>
    public partial class SampleUserControl : deFramework.Base.BaseUserControl
    {

        #region "Customizable Variables + supporting members"

        private int LoopCount = 3;
        private string LoopMessage = "Unspecified Loop Message";
        private string SystemErrorLabel = "Unspecified SYSTEM ERROR";


        private string kDefaultUserControlParameters =
            "<V1><LoopCount>5</LoopCount><LoopMessage>Hello World</LoopMessage><SystemErrorLabel>SYSTEM ERROR</SystemErrorLabel></V1>";

        private bool ValidateCustomUserControlConfiguration()
        {
            bool _result = false;
            try
            {
                // Use a version number for your customizable user control variables in order to safely manage backwards compatibility 
                if (this.UserControlParameters.StartsWith("<V1>"))
                {
                    // For each customizable variable make a call to a method for its data type to allow for web part customization.  
                    this.LoopCount = this.GetIntOverrideConfigVariableWithUserControlParamValue(
                        "LoopCount", this.LoopCount);
                    this.LoopMessage = this.GetOverrideConfigVariableWithUserControlParamValue(
                        "LoopMessage", this.LoopMessage);
                    this.SystemErrorLabel = this.GetOverrideConfigVariableWithUserControlParamValue(
                        "SystemErrorLabel", this.SystemErrorLabel);

                    _result = true;
                }
                else if (this.UserControlParameters.Trim().ToLower() == "help")
                    this.PanelContent.InnerHtml = this.UserControlParametersHelpContent();
                else if (this.UserControlParameters.Length > 1)
                    this.PanelContent.InnerHtml = "Web Part Configuration requires User Control Parameters setting to use V1 parameters set. Please contact your Site Owner.";
            }
            catch { }
            return _result;
        }

        /// <summary>
        /// Only customize the lineXX string value assignments and the list of line parameters in the return String.Format 
        /// command of this method to provide user control specific help for customizing parameter values.  
        /// </summary>
        /// <returns></returns>
        private string UserControlParametersHelpContent()
        {
            string lineHead1 = "<H4>CMAP/Alex1Demo.ascx &nbsp; &nbsp; User  Control  Parameters  Help</H4>";
            string lineHead2 = "<table border=1><tr><td><b>Variable</b></td><td><b>Description</b></td><td><b>Example</b></td></tr>";
            string line1 = "<tr><td>LoopCount</td><td>Number of times to loop through message display.</td><td>5</td></tr>";
            string line2 = "<tr><td>RefreshSeconds</td><td>Text that displays in each message display.</td><td>Hello World</td></tr>";
            string line3 = "<tr><td>SystemErrorLabel</td><td>If an error/exception occurs then this setting specifies what the end user will see.</td><td>SYSTEM ERROR</td></tr>";
            string lineFoot = "</table>&nbsp;&nbsp;<b>Example</b>:&nbsp;<input type=text size=120 value=\"" + this.kDefaultUserControlParameters + "\" />";
            string kHelp = "&nbsp;<br />{0}&nbsp;<br />{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}<br />&nbsp;<br />{11}&nbsp;<br />&nbsp;";
            return String.Format(kHelp, lineHead1 + lineHead2,
                line1, line2, line3,
                lineFoot);
        }

        #endregion 
  
        #region "OnInit, OnLoad, ProcessingForOnLoad"


        /// <summary>
        /// Do not change this method.     
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            this.UserControlParameters = this.kDefaultUserControlParameters;
            base.OnInit(e);
        }


        /// <summary>
        /// Do not change this method. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                if (this.ValidateCustomUserControlConfiguration())
                {
                    this.ProcessingForOnLoad();
                }
                else
                    this.ShowErrorInPanel();
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
                this.ShowErrorInPanel();
            }
        }

        /// <summary>
        /// This is where the user control logic for the OnLoad event should go. 
        /// </summary>
        private void ProcessingForOnLoad()
        {
            this.SampleDisplayProcessing(
                this.LoopCount,
                this.LoopMessage,
                this.SystemErrorLabel
                );
        }

        #endregion

        private void SampleDisplayProcessing(int iterationsCount, string iterationMessage, string aErrorMessage)
        {
            try
            {
                if (iterationsCount > 0)
                {
                    for (int i = 0; i < iterationsCount; i++)
                        this.PanelContent.InnerHtml += "<P>&nbsp;Message:&nbsp;" + iterationMessage;
                }
                else
                {
                    this.PanelContent.InnerHtml = "Error Message: " + aErrorMessage;
                }
            }
            catch (Exception ex)
            {
                this.CurrentException = new Exception(aErrorMessage, ex);
            }
        }
         

    }
}




#region "User Control Codebehind Template for User Controls created by the de Control Container Web Part"

// 1. Prepare the new user control by doing the following steps: 
//    A. Create the user control in the Template/ControlTemplates/FEATURE-NAME directory of our WSP project. 
//    B. Make sure the user control partial class inherits from deFramework.Base.BaseUserControl. 
//    C. Be sure that the .ascx content of the user control contains a DIV tag with an ID of divContent 
//       to support the default error handling. If you give the panel a different ID then set the 
//       PanelContentControlName property of the user control partial class to be its ID.
//    D. Copy this region for the Codebehind Template into the main body of the user control partial class.    
//    E. Do an initial uncommenting of all the contents in this "Commented Code Regions to Uncomment" 
//       region and then complete steps 2-10 that are listed within this commented region.

    #region "Commented Code Regions to Uncomment"

//#region "Steps to Implement Customizable Variables"


//// 2. Specify the customizable variables for this User Control within this region. 

//#region "Customizable Variables"

//private int LoopCount = 3;
//private string LoopMessage = "Unspecified Loop Message";
//private string SystemErrorLabel = "Unspecified SYSTEM ERROR";

//#endregion


///// <summary>
///// 3. Customize this value for your user control to provide default values for all your customizable variables. 
///// </summary>
//private string kDefaultUserControlParameters =
//    "<V1><LoopCount>5</LoopCount><LoopMessage>Hello World</LoopMessage><SystemErrorLabel>SYSTEM ERROR</SystemErrorLabel></V1>";




///// <summary>
///// Make changes only in certain places here 
///// </summary>
///// <returns></returns>
//private bool ValidateCustomUserControlConfiguration()
//{
//    bool _result = false;
//    try
//    {
//        // 4. Use a version number for your customizable user control variables in order to safely manage backwards compatibility 

//        if (this.UserControlParameters.StartsWith("<V1>"))
//        {
//            // 5. For each customizable variable make a call to a method for its data type to allow for web part customization.  
//            this.LoopCount = this.GetIntOverrideConfigVariableWithUserControlParamValue(
//                "LoopCount", this.LoopCount);
//            this.LoopMessage = this.GetOverrideConfigVariableWithUserControlParamValue(
//                "LoopMessage", this.LoopMessage);
//            this.SystemErrorLabel = this.GetOverrideConfigVariableWithUserControlParamValue(
//                "SystemErrorLabel", this.SystemErrorLabel);

//            _result = true;
//        }
//        else if (this.UserControlParameters.Trim().ToLower() == "help")
//            this.PanelContent.InnerHtml = this.UserControlParametersHelpContent();
//        else if (this.UserControlParameters.Length > 1)
//            this.PanelContent.InnerHtml = "Web Part Configuration requires User Control Parameters setting to use V1 parameters set. Please contact your Site Owner.";
//    }
//    catch { }
//    return _result;
//}

///// <summary>
///// 6. Only customize the lineXX string value assignments and the list of line parameters in the return String.Format 
///// command of this method to provide user control specific help for customizing parameter values.  
///// </summary>
///// <returns></returns>
//private string UserControlParametersHelpContent()
//{
//    string lineHead1 = "<H4>CMAP/Alex1Demo.ascx &nbsp; &nbsp; User  Control  Parameters  Help</H4>";
//    string lineHead2 = "<table border=1><tr><td><b>Variable</b></td><td><b>Description</b></td><td><b>Example</b></td></tr>";
//    string line1 = "<tr><td>LoopCount</td><td>Number of times to loop through message display.</td><td>5</td></tr>";
//    string line2 = "<tr><td>RefreshSeconds</td><td>Text that displays in each message display.</td><td>Hello World</td></tr>";
//    string line3 = "<tr><td>SystemErrorLabel</td><td>If an error/exception occurs then this setting specifies what the end user will see.</td><td>SYSTEM ERROR</td></tr>";
//    string lineFoot = "</table>&nbsp;&nbsp;<b>Example</b>:&nbsp;<input type=text size=120 value=\"" + this.kDefaultUserControlParameters + "\" />";
//    string kHelp = "&nbsp;<br />{0}&nbsp;<br />{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}<br />&nbsp;<br />{11}&nbsp;<br />&nbsp;";
//    return String.Format(kHelp, lineHead1 + lineHead2,
//        line1, line2, line3,
//        lineFoot);
//}

//// 7. Do not change the OnInit and OnLoad methods below. Your intial user control specific code should be done in the
////    ProcessingForOnLoad method that gets called in the OnLoad method if the variables customization process 
////    validates/completes successfully.  

//// 8. At design-time on your web parts page in your de Control Container web part properties set 
////    the property values as follows: 
////      UserControlPath:       ~/_controltemplates/FEATURE-NAME/USER-CONTROL-NAME.ascx
////      UserControlParameters: help 
////    Be sure to substitute FEATURE-NAME and USER-CONTROL-NAME with appropriate values for this user control. 

//// 9. Once the help renders and based on your desired customizations for the customizable variables 
////    make the changes to the UserControlParameters by following the help display.  

//// 10. Do cleanup on any of these instructions that you no longer need once you have setup your user control 
////     with the CCWP and established/implemented your customizable variables. 

//#endregion

//#region "OnInit, OnLoad, ProcessingForOnLoad"


///// <summary>
///// Do not change this method.     
///// </summary>
///// <param name="e"></param>
//protected override void OnInit(EventArgs e)
//{
//    this.UserControlParameters = this.kDefaultUserControlParameters;
//    base.OnInit(e);
//}


///// <summary>
///// Do not change this method. 
///// </summary>
///// <param name="e"></param>
//protected override void OnLoad(EventArgs e)
//{
//    base.OnLoad(e);
//    try
//    {
//        if (this.ValidateCustomUserControlConfiguration())
//        {
//            this.ProcessingForOnLoad();
//        }
//        else
//            this.ShowErrorInPanel();
//    }
//    catch (Exception ex)
//    {
//        this.CurrentException = ex;
//        this.ShowErrorInPanel();
//    }
//}

///// <summary>
///// This is where the user control logic for the OnLoad event should go. 
///// </summary>
//private void ProcessingForOnLoad()
//{
//    this.SampleDisplayProcessing(
//        this.LoopCount,
//        this.LoopMessage,
//        this.SystemErrorLabel
//        ); 
//}

//#endregion 

//private void SampleDisplayProcessing(int iterationsCount, string iterationMessage, string aErrorMessage)
//{
//    try
//    {
//        if (iterationsCount > 0)
//        {
//            for (int i = 0; i < iterationsCount; i++)
//                this.PanelContent.InnerHtml += "<P>&nbsp;Message:&nbsp;" + iterationMessage;
//        }
//        else
//        {
//            this.PanelContent.InnerHtml = "Error Message: " + aErrorMessage;
//        }
//    }
//    catch (Exception ex)
//    {
//        this.CurrentException = new Exception(aErrorMessage, ex); 
//    }
//}


#endregion

#endregion

